package com.yh.correct.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yh.correct.dao.ICorrectedPersonMapper;
import com.yh.correct.dao.IUserMapMapper;
import com.yh.correct.model.CorrectedPerson;
import com.yh.correct.model.Crawl;
import com.yh.correct.model.JudicialDeptment;
import com.yh.correct.model.LeavePersonnel;
import com.yh.correct.model.Page;
import com.yh.correct.model.RuleBreak;
import com.yh.correct.model.UserMap;
import com.yh.correct.service.IUserMapService;
import java.awt.geom.Point2D;

@Service
public class IUserMapServiceImpl implements IUserMapService {
	private static Logger logger = LogManager.getLogger(IUserMapServiceImpl.class);
	@Autowired
	// 注入数据层地图数据对象
	private IUserMapMapper iusermapmapper;

	@Override
	/**
	 * 添加对象相应信息 (non-Javadoc)
	 * 
	 * @see com.yh.correct.service.IUserMapService#addUserMap(com.yh.correct.model.
	 *      UserMap)
	 */
	public int addUserMap(UserMap map) throws Exception {
		// TODO Auto-generated method stub
		logger.error("添加：" + map);
		return iusermapmapper.addUserMap(map);
	}

	/**
	 * 根据指定条件（id）删除对象数据 (non-Javadoc)
	 * 
	 * @see com.yh.correct.service.IUserMapService#delUserMap(java.lang.Integer)
	 */
	@Override
	public int delUserMap(Integer id) throws Exception {
		// TODO Auto-generated method stu
		logger.error("删除id为：" + id + "用户人员");
		return iusermapmapper.delUserMap(id);
	}

	/**
	 * 修改对象信息 (non-Javadoc)
	 * 
	 * @see com.yh.correct.service.IUserMapService#editUserMap(com.yh.correct.model.
	 *      UserMap)
	 */
	@Override
	public int editUserMap(UserMap map) throws Exception {
		// TODO Auto-generated method stub
		logger.error("修改：" + map);
		return iusermapmapper.editUserMap(map);
	}

	/**
	 * 根据 选定对象对其进行 定位 (non-Javadoc)
	 * 
	 * @see com.yh.correct.service.IUserMapService#getUser(com.yh.correct.model.
	 *      CorrectedPerson)
	 */
	@Override
	public UserMap getUser(CorrectedPerson person) throws Exception {
		// TODO Auto-generated method stub
		return iusermapmapper.getUser(person);
	}

	/**
	 * 查看某对象在 某 时间段的 行动轨迹 (non-Javadoc)
	 * 
	 * @see com.yh.correct.service.IUserMapService#getUsers(java.util.Map)
	 */
	@Override
	public List<UserMap> getUsers(Map map) throws Exception {
		// TODO Auto-generated method stub
		return iusermapmapper.getUsers(map);
	}




	@Override
	public List<JudicialDeptment> getJudicialDeptment() throws Exception {
		// TODO Auto-generated method stub
		return iusermapmapper.getJudicialDeptment();
	}

	@Override
	public List<CorrectedPerson> getJudicialDeptments(JudicialDeptment judicialDeptment) throws Exception {
		// TODO Auto-generated method stub
		return iusermapmapper.getJudicialDeptments(judicialDeptment);
	}

	@Override
	public Page queryLike(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		logger.debug("模糊查询" + condition);
		Page page = new Page();
		// 根据条件查询符合的用户列表记录总数，赋值给page的totalNum变量
		page.setTotalNum(iusermapmapper.getLikeUsersCount(condition));
		// 从请求参数中获取每页大小
		int pageSize = Integer.parseInt(String.valueOf(condition.get("pageSize")));
		page.setPageSize(pageSize);
		// 从请求参数中获取当前页码
		int curPageNum = Integer.parseInt(String.valueOf(condition.get("pageNum")));
		page.setCurPageNum(curPageNum);
		// 动态计算总页数(总记录数 除以 每页大小，加上 总页数 求余 每页大小，如果余数不为0，则 加 1，否则 加 0 )
		page.setTotalPage(page.getTotalNum() / pageSize + (page.getTotalNum() % pageSize == 0 ? 0 : 1));
		// 根据条件查询符合的用户列表记录，赋值给page的result变量
		page.setResult(iusermapmapper.getLikeUsers(condition));

		return page;
	}

	@Override
	public UserMap getUserss(CorrectedPerson person) throws Exception {
		// TODO Auto-generated method stub
		return iusermapmapper.getUserss(person);
	}
	/**
	 * 根据对象信息查看对象位置信息
	 * 
	 */
	public List<Object> getLocation(CorrectedPerson person) throws Exception{
		return iusermapmapper.getLocation(person);
	}
	/**
	 * 添加对象范围
	 * 
	 */
	@Override
	public int addMap(Crawl arawl) throws Exception {
		// TODO Auto-generated method stub
		return iusermapmapper.addMap(arawl);
	}
	/**
	 * 得到对象范围
	 * 
	 */
	@Override
	public List<Crawl> getAll(CorrectedPerson person) throws Exception {
		// TODO Auto-generated method stub
		return iusermapmapper.getAll(person);
	}

	@Override
	public List<UserMap> getAlls() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 模糊查询电子围墙信息
	 * 
	 * @param condition
	 * @return
	 */
	@Override
	public Page getLikeWall(Map<String, Object> condition) throws Exception {
		Page page=new Page();
		page.setTotalNum(iusermapmapper.getLikeWallCount(condition));
		int pageSize = Integer.parseInt(String.valueOf(condition.get("pageSize")));
		page.setPageSize(pageSize);
		int curPageNum = Integer.parseInt(String.valueOf(condition.get("pageNum")));
		page.setCurPageNum(curPageNum);
		page.setTotalPage(page.getTotalNum()/pageSize+(page.getTotalNum()%pageSize==0?0:1));
		page.setResult(iusermapmapper.getLikeWall(condition));
		logger.debug("模糊查询电子围墙信息!");
		return page;
	}
	/**
	 * 查询所有电子围墙信息
	 * 
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getAllWalls() throws Exception {
		return iusermapmapper.getAllWalls();
	}
	/**
	 * 根据对象信息查看对象短信信息
	 * 
	 */
	@Override
	public List<Object> getMessage(CorrectedPerson person) throws Exception {
		// TODO Auto-generated method stub
		return iusermapmapper.getMessage(person);
	}
	/**
	 * 查看所有人员信息
	 * 
	 */
	@Override
	public List<CorrectedPerson> getAllUser() throws Exception {
		// TODO Auto-generated method stub
		return iusermapmapper.getAllUser();
	}
	/**
	 * 判断范围
	 * 
	 * @return
	 */
	 public boolean IsPtInPoly(Point2D.Double point, List<Point2D.Double> pts){
	    
	    int N = pts.size();
	    boolean boundOrVertex = true; //如果点位于多边形的顶点或边上，也算做点在多边形内，直接返回true
	    int intersectCount = 0;//cross points count of x 
	    double precision = 2e-10; //浮点类型计算时候与0比较时候的容差
	    Point2D.Double p1, p2;//neighbour bound vertices
	    Point2D.Double p = point; //当前点
	    
	    p1 = pts.get(0);//left vertex        
	    for(int i = 1; i <= N; ++i){//check all rays            
	        if(p.equals(p1)){
	            return boundOrVertex;//p is an vertex
	        }
	        
	        p2 = pts.get(i % N);//right vertex            
	        if(p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)){//ray is outside of our interests                
	            p1 = p2; 
	            continue;//next ray left point
	        }
	        
	        if(p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)){//ray is crossing over by the algorithm (common part of)
	            if(p.y <= Math.max(p1.y, p2.y)){//x is before of ray                    
	                if(p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)){//overlies on a horizontal ray
	                    return boundOrVertex;
	                }
	                
	                if(p1.y == p2.y){//ray is vertical                        
	                    if(p1.y == p.y){//overlies on a vertical ray
	                        return boundOrVertex;
	                    }else{//before ray
	                        ++intersectCount;
	                    } 
	                }else{//cross point on the left side                        
	                    double xinters = (p.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;//cross point of y                        
	                    if(Math.abs(p.y - xinters) < precision){//overlies on a ray
	                        return boundOrVertex;
	                    }
	                    
	                    if(p.y < xinters){//before ray
	                        ++intersectCount;
	                    } 
	                }
	            }
	        }else{//special case when ray is crossing through the vertex                
	            if(p.x == p2.x && p.y <= p2.y){//p crossing over p2                    
	                Point2D.Double p3 = pts.get((i+1) % N); //next vertex                    
	                if(p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)){//p.x lies between p1.x & p3.x
	                    ++intersectCount;
	                }else{
	                    intersectCount += 2;
	                }
	            }
	        }            
	        p1 = p2;//next ray left point
	    }
	    
	    if(intersectCount % 2 == 0){//偶数在多边形外
	        return false;
	    } else { //奇数在多边形内
	        return true;
	    }
	    
	}
	/**
	 * 根据对象信息查看对象违规信息
	 * 
	 */
	@Override
	public List<Object> getRuleBreak(CorrectedPerson person) throws Exception {
		// TODO Auto-generated method stub
		return iusermapmapper.getRuleBreak(person);
	}
	/**
	 * 根据对象信息查看对象请假信息
	 * 
	 */
	@Override
	public List<Object> getLeavePersonnel(CorrectedPerson person) throws Exception {
		// TODO Auto-generated method stub
		return iusermapmapper.getLeavePersonnel(person);
	}





}
